<template>
  <div class="container">
    <div>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form ref="formSearch" :model="formSearch">
            <el-form-item label="会议室名称：">
              <el-input
                style="width: 150px"
                v-model="formSearch.name"
                placeholder="请输入"
                class="handle-input mr10"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-form style="float: left">
            <el-button size="mini" type="primary" @click="personQuery"
              >查 询</el-button
            >
            <el-button size="mini" type="success" @click="showAddDialog"
              >新 增</el-button
            >
          </el-form>
        </el-col>
      </el-row>
    </div>
    <el-table
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      highlight-current-row
    >
      <el-table-column
        width="300"
        prop="会议室名称"
        label="会议室名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="会议室简介"
        label="会议室简介"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="showUpdateDialog(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            style="color: #ff0000; margin-left: 35px"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="tableData.length"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>

    <!-- 弹出框 -->
    <el-dialog
      :title="formDialog.title"
      :visible.sync="formDialog.show"
      width="40%"
    >
      <el-form
        ref="formDialog"
        :model="formDialog"
        label-width="100px"
        class="flex"
      >
        <!-- 会议室名称,会议室简介,密码,机构编号,机构名称,楼宇名称,类型 -->
        <el-form-item label="会议室名称：">
          <el-input
            style="width: 200px"
            v-model="formDialog.NAME"
            placeholder="请输入"
            class="handle-input mr10"
          ></el-input>
        </el-form-item>
        <el-form-item label="会议室简介：">
          <el-input
            style="width: 200px"
            v-model="formDialog.DETAIL"
            placeholder="请输入"
            class="handle-input mr10"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="formDialog.show = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import '@/assets/css/common.css'
export default {
  data () {
    return {
      currentPage: 1, //初始页
      pageSize: 10,    //    每页的数据
      formDialog: {},
      formSearch: {
        name: '',
        orgID: '',
        type: '',
      },
      tableData: [],
      originalData: [],
      userTypeList: [{ "label": "用户", "value": "0" }, { "label": "物业人员", "value": "1" }]
    }
  },
  mounted () {
    this.getData()
  },
  methods: {
    // 获取 会议室
    async getData () {
      var url = 'System/GetMeetIngRoomInfo?Name=&UserID=' + localStorage.getItem('UserID')
      const { data } = await this.$Http.get(url)
      this.originalData = data
      // console.log(data)
      this.personQuery()
    },
    // 模糊查询
    personQuery () {
      this.currentPage = 1;
      this.tableData = this.originalData
      if (this.formSearch.name) this.tableData = this.tableData.filter(item => item.会议室名称.includes(this.formSearch.name))
    },
    // 组织机构 新增
    handleSubmit () {
      if (this.formDialog.title.includes('新增')) {
        this.addData();
      } else {
        this.updateData();
      }
    },
    updateData () {
      this.$axios({
        url: 'System/UpdateMeetIngRoomInfo',
        method: 'post',
        data: this.formDialog
      }).then(res => {
        console.log(res)
        if (res.data.IsSucess) {
          this.$message.success('编辑成功')
          this.formDialog.show = false;
          this.getData()
        } else {
          this.$message.error('编辑失败' + '，' + res.data.Message);
        }
      })
    },
    addData () {
      this.formDialog.CLIENT_NAME = localStorage.getItem('DepartmentName')
      this.$axios({
        url: 'System/AddMeetIngRoomInfo',
        method: 'post',
        data: this.formDialog
      }).then(res => {
        console.log(res)
        if (res.data.IsSucess) {
          this.$message.success('新增成功')
          this.formDialog.show = false;
          this.getData()
        } else {
          this.$message.error('新增失败' + '，' + res.data.Message);
        }
      })
    },
    // 打开新增窗口
    showAddDialog () {
      //清空编辑框
      this.formDialog = {
        title: '新增人员',
        show: true,
        NAME: '',
        DETAIL: '',
      }
    },
    // 打开新增窗口
    showUpdateDialog (index, row) {
      var userData = Object.assign({}, row);
      this.formDialog = {
        title: '编辑人员',
        show: true,
        NAME: userData.会议室名称,
        DETAIL: userData.会议室简介,

        ID: userData.ID,
      }
    },
    // 删除
    async handleDelete (index, row) {
      var url = 'System/DeleteMeetIngRoomInfo?MeetIngRoomID=' + row.ID
      const { data } = await this.$Http.get(url)
      if (data.IsSucess) {
        this.$message.success('删除成功')
        this.getData()
      } else {
        this.$message.error('删除失败');
      }
    },
    // 分页导航
    handlePageChange (currentPage) {
      this.currentPage = currentPage;
      // console.log(this.currentPage)
    },
    handleSizeChange (size) {
      this.pageSize = size;
      // console.log(this.pageSize)
    }
  }
}
</script>
<style>
</style>